package com.orangelife.mobile.parkingcard.activity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.sp.UserSp;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.TimeUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.common.jump.BaseTipJumper;
import com.orangelife.mobile.parkingcard.biz.ParkingCardBiz;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 使用权转让
 * <p/>
 * Created by wgf on 2016/7/5.
 *
 * @author wgf
 * @since v3.8
 */
public class GrantPermissionActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.etName)
    EditText etName;
    @Bind(R.id.etTel)
    EditText etTel;
    @Bind(R.id.tvDateStart)
    TextView tvDateStart;
    @Bind(R.id.tvDateEnd)
    TextView tvDateEnd;
    @Bind(R.id.button)
    Button button;


    /** 用于提交的有效期起始日期 */
    private String timeStampStart = "";
    private String timeStampEnd = "";

    /** DatePicker使用的年月日 */
    private int yearStart = 2016;
    private int monthOfYearStart = 0;
    private int dayOfMonthStart = 1;
    private int yearEnd = 2016;
    private int monthOfYearEnd = 0;
    private int dayOfMonthEnd = 1;

    /** 认证ID */
    private String linkAptHouseholderID;

    //    private String ownerID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grant_permission);
        ButterKnife.bind(this);
        initDate();
        initView();
        getIntentInfo();

    }

    private void getIntentInfo() {
        linkAptHouseholderID = getIntent().getStringExtra("linkAptHouseholderID");
//        ownerID=getIntent().getStringExtra("ownerID");
    }



    private void initView() {
        tvTitle.setText("使用权转让");
    }

    private void initDate() {
        timeStampStart = String.valueOf(System.currentTimeMillis());
        timeStampEnd = String.valueOf(System.currentTimeMillis());
        String date = TimeUtil.timeStampToDate(System.currentTimeMillis(), "yyyy-MM-dd");
        tvDateStart.setText(date);
        tvDateEnd.setText(date);
        if (date != null) {
            String[] s = date.split("-");
            yearStart = Integer.parseInt(s[0]);
            monthOfYearStart = Integer.parseInt(s[1])-1;
            dayOfMonthStart = Integer.parseInt(s[2]);
            yearEnd = yearStart;
            monthOfYearEnd = monthOfYearStart;
            dayOfMonthEnd = dayOfMonthStart;
        }
    }

    private void requestInvite() {
        String name = etName.getText().toString();
        String tel = etTel.getText().toString();
        if (!ParkingCardBiz.validate(this, name, tel,timeStampStart,timeStampEnd, linkAptHouseholderID)) {
            return;
        }
        showDialog(this);
        ParkingSpaceHttpBiz.addUser(name, tel, timeStampStart, timeStampEnd, linkAptHouseholderID, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject jsonObject, int i) {
                closeDialog();
                if (OrangeErrorHandler.getInstance().isSuccess(jsonObject)) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", linkAptHouseholderID);
                    BaseTipJumper.jumpToBaseTip(GrantPermissionActivity.this, bundle, "", "新增授权", "使用权转让", "授权成功！");
                }
            }
        });
    }


    private void showDatePicker(final boolean isStart) {
        int year;
        int monthOfYear;
        int dayOfMonth;
        if (isStart) {
            year = yearStart;
            monthOfYear = monthOfYearStart;
            dayOfMonth = dayOfMonthStart;
        } else {
            year = yearEnd;
            monthOfYear = monthOfYearEnd;
            dayOfMonth = dayOfMonthEnd;
        }
        //DatePickerDialog返回的为当天0点的时间戳
        DatePickerDialog tpd = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                saveTimeStamp(isStart, year, monthOfYear, dayOfMonth);
            }
        }, year, monthOfYear, dayOfMonth
        );
        tpd.show();
    }

    private void saveTimeStamp(boolean isStart, int year, int monthOfYear, int dayOfMonth) {
        String formatDate = year + "-" + ParkingCardBiz.addZero((monthOfYear + 1)) + "-" + ParkingCardBiz.addZero(dayOfMonth);
        String s = String.valueOf(ParkingCardBiz.parseDate(formatDate));
        if (isStart) {
            yearStart = year;
            monthOfYearStart = monthOfYear;
            dayOfMonthStart = dayOfMonth;
            timeStampStart = s;
        } else {
            yearEnd = year;
            monthOfYearEnd = monthOfYear;
            dayOfMonthEnd = dayOfMonth;
            timeStampEnd = s;
        }
        setNewTimeToTextView(isStart, formatDate);
    }

    private void setNewTimeToTextView(boolean isStart, String formatDate) {
        if (isStart) {
            tvDateStart.setText(formatDate);
        } else {
            tvDateEnd.setText(formatDate);
        }
    }


    @OnClick({R.id.button, R.id.tvDateStart, R.id.tvDateEnd})
    public void onClick(View view) {
        switch (view.getId()) {//
            case R.id.button:
                requestInvite();
                break;
            case R.id.tvDateStart:
//                Toast.makeText(GrantPermissionActivity.this, "tvDateStart", Toast.LENGTH_SHORT).show();
                showDatePicker(true);
                break;
            case R.id.tvDateEnd:
                showDatePicker(false);
                break;
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//            finish();
//            IntentHelper.getInstances().intentWithExtraString(this,ParkingSpaceManagementActivity.class,"ownerID",ownerID);
//            return true;
//        } else
//            return super.onKeyDown(keyCode, event);
//    }

}
